package com.sqx.modules.evaluate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserCertification;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserCertificationService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.evaluate.dao.SysEvaluateDao;
import com.sqx.modules.evaluate.entity.SysEvaluate;
import com.sqx.modules.evaluate.service.SysEvaluateService;
import com.sqx.modules.orders.entity.Orders;
import com.sqx.modules.orders.service.OrdersService;
import com.sqx.modules.task.dao.HelpOrderDao;
import com.sqx.modules.task.entity.HelpOrder;
import com.sqx.modules.task.entity.HelpTake;
import com.sqx.modules.task.service.HelpTakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wuchang
 * @since 2022-11-10
 */
@Service
public class SysEvaluateServiceImpl extends ServiceImpl<SysEvaluateDao, SysEvaluate> implements SysEvaluateService {
    @Autowired
    private SysEvaluateDao evaluateDao;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;
    @Autowired
    private HelpOrderDao helpOrderDao;
    @Autowired
    private HelpTakeService takeService;
    @Autowired
    private UserCertificationService userCertificationService;

    @Override
    public Result addEvaluate(SysEvaluate evaluate) {
        evaluate.setCreateTime(new Date());
        //万能任务
        if (evaluate.getEvaluateType() == 1) {
            HelpOrder helpOrder = helpOrderDao.selectOne(new QueryWrapper<HelpOrder>().eq("order_no", evaluate.getIndentNumber()));
            if (helpOrder == null) {
                return Result.error("当前订单不存在");
            }
            if (helpOrder.getStatus() != 4) {
                return Result.error("当前订单未完成");
            }
            HelpTake helpTake = takeService.getById(helpOrder.getHelpTakeId());
            evaluate.setRiderUserId(helpTake.getUserId());
            //派单任务
        } else {
            Orders orders = ordersService.getOne(new QueryWrapper<Orders>().eq("orders_no", evaluate.getIndentNumber()));
            if (!"2".equals(orders.getState())) {
                return Result.error("当前订单未完成");
            }
            evaluate.setRiderUserId(orders.getOrderTakingUserId());
        }
        evaluateDao.insert(evaluate);

        /*UserEntity userEntity = userService.getById(evaluate.getRiderUserId());
        if (userEntity == null) {
            return Result.error("当前用户不存在");
        }
        //总分数
        String totalScore = evaluateDao.getTotalScore(userEntity.getUserId());
        //评论总条数
        Integer count = evaluateDao.selectCount(new QueryWrapper<SysEvaluate>().eq("rider_user_id", userEntity.getUserId()));
        //更新用户评分
        BigDecimal score = new BigDecimal(totalScore).divide(new BigDecimal(count), 1, BigDecimal.ROUND_DOWN);
        userEntity.setFinalScore(score.toString());
        userService.updateById(userEntity);
        UserCertification userCertification = userCertificationService.getOne(new QueryWrapper<UserCertification>().eq("user_id", userEntity.getUserId()));
        if(userCertification!=null){
            userCertification.setScore(score.doubleValue());
            userCertificationService.updateById(userCertification);
        }*/
        return Result.success();
    }

    @Override
    public IPage<SysEvaluate> getUserEvaluateList(Integer page, Integer limit, SysEvaluate evaluate) {
        Page<SysEvaluate> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return evaluateDao.getUserEvaluateList(pages, evaluate);

    }

    @Override
    public Result deleteEvaluate(Long userId, Long evaluateId, Integer auth) {
        SysEvaluate sysEvaluate = evaluateDao.selectById(evaluateId);
        if (auth != 1) {
            if (!userId.equals(Long.parseLong(sysEvaluate.getUserId()))) {
                return Result.error("您无法操作当前评论");
            }
        }

        return Result.upStatus(evaluateDao.deleteById(evaluateId));

    }

    @Override
    public Result examine(Long evaluateId, Integer status, String remarks) {
        SysEvaluate sysEvaluate = baseMapper.selectById(evaluateId);
        sysEvaluate.setStatus(status);
        sysEvaluate.setRemarks(remarks);
        int i = baseMapper.updateById(sysEvaluate);
        if(status==1){
            UserEntity userEntity = userService.getById(sysEvaluate.getRiderUserId());
            if (userEntity != null) {
                //总分数
                String totalScore = evaluateDao.getTotalScore(userEntity.getUserId());
                //评论总条数
                Integer count = evaluateDao.selectCount(new QueryWrapper<SysEvaluate>().eq("status",1).eq("rider_user_id", userEntity.getUserId()));
                //更新用户评分
                BigDecimal score = new BigDecimal(totalScore).divide(new BigDecimal(count), 1, BigDecimal.ROUND_DOWN);
                userEntity.setFinalScore(score.toString());
                userService.updateById(userEntity);
                UserCertification userCertification = userCertificationService.getOne(new QueryWrapper<UserCertification>().eq("user_id", userEntity.getUserId()));
                if(userCertification!=null){
                    userCertification.setScore(score.doubleValue());
                    userCertificationService.updateById(userCertification);
                }
            }
        }

        return Result.upStatus(i);
    }

    @Override
    public BigDecimal selectCommentOrderScoreByUserId(Long userId,String startTime,String endTime){
        return baseMapper.selectCommentOrderScoreByUserId(userId,startTime,endTime);
    }

    @Override
    public Integer selectCommentOrderCountByUserId(Long userId,String startTime,String endTime){
        return baseMapper.selectCommentOrderCountByUserId(userId,startTime,endTime);
    }



}
